package com.pushmessgae.server.log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

public final class Log {

	private static final Log LOG = new Log();
	
	private static final String LEVEL_DEBUG ="debug";
	
	private Vector<LogListener> listeners = new Vector<Log.LogListener>();
	
	private Object object = new Object();
	
	public static void d(String tag , String message){
//		System.out.println(getCurrentTime() + "\t" + tag + "\t" + message);
		getInstance().outLog(LEVEL_DEBUG, getCurrentTime(), tag, message);
	}
	
	public static Log getInstance(){
		return LOG;
	}
	
	public void outLog(String level , String time, String tag , String message){
		LogInfo logInfo = new  LogInfo(level, time, tag, message);
		notfiyLogListener(logInfo );
	}
	
	private static String getCurrentTime(){
		SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss.SS");
		return format.format(new Date());
	}
	
	private void notfiyLogListener(LogInfo logInfo){
		synchronized (object) {
			for(LogListener listener : listeners){
				listener.onLog(logInfo);
			}
		}
	}
	
	public void addLogListener(LogListener logListener){
		synchronized (object) {
			listeners.add(logListener);
		}
	}
	
	public interface LogListener {
		void onLog(LogInfo logInfo);
	}
	
	public final class LogInfo {
		
		private String level;
		
		private String time;
		
		private String tag;
		
		private String message;

		public LogInfo(String level , String time, String tag, String message) {
			super();
			this.time = time;
			this.tag = tag;
			this.message = message;
		}

		public String getLevel() {
			return level;
		}

		public void setLevel(String level) {
			this.level = level;
		}

		public String getTime() {
			return time;
		}

		public void setTime(String time) {
			this.time = time;
		}

		public String getTag() {
			return tag;
		}

		public void setTag(String tag) {
			this.tag = tag;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}
		
	}
	
}
